package com.example.blog.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.example.blog.entity.Article;
import com.example.blog.entity.User;
import com.example.blog.mapper.ArticleMapper;
import com.example.blog.service.IArticleService;
import com.ly.common.bean.Result;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import sun.reflect.generics.tree.Tree;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ly
 * @since 2023-08-03
 */
@Controller
@RequestMapping("/blog/article")
public class ArticleController {

    @Resource
    ArticleMapper articleMapper;

    @GetMapping("queryNew")
    @ResponseBody
    public IPage<Article> queryNew(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size,
            String label, String title) {
        int begin = (page - 1) * size;
        List<Article> list = articleMapper.selectNews(begin, size,label,title);
        Page<Article> p = new PageDTO<>(page, size);
        p.setRecords(list);
        return p;
    }

    @Resource
    RedisTemplate redisTemplate;

    @GetMapping("queryById")
    @ResponseBody
    public Article queryById(Article article) {
        article = articleMapper.findByid(article.getId());

//        HashOperations hash = redisTemplate.opsForHash();
//        Integer o = (Integer) hash.get("article-readCnt", article.getId());

        article.setReadCnt(article.getReadCnt() == null ? 1 : article.getReadCnt() + 1);
        articleMapper.updateById(article);
        return article;
    }

    @Resource
    IArticleService articleService;

    @RequestMapping("submit")
    @ResponseBody
    public Result submit(@RequestBody Article article,
                         @SessionAttribute User myinfo) {
        // 作者
        article.setAuthor(myinfo.getName());
        // saveOrUpdate 新增或修改
        articleService.saveOrUpdate(article);
        return new Result(1, "文章提交成功!", null);
    }

    @RequestMapping("queryLabels")
    @ResponseBody
    public Map<String, Integer> queryLabels(){
        Map<String, Integer> ret = new TreeMap<>();
        Map<String, Object> map = articleMapper.selectLabels();
        String labels = (String) map.get("labels");
        for (String label : labels.split("[,\\s]+")) {
            Integer nums = ret.get(label);
            if(nums == null){
                nums = 1;
            } else {
                nums ++;
            }
            ret.put(label,nums);
        }
        return ret;
    }

    // 点赞
    @RequestMapping("argee")
    @ResponseBody
    Result argee(int aid, @SessionAttribute User myinfo){
        SetOperations set = redisTemplate.opsForSet();
        set.add("article-argee:"+ aid, myinfo.getId());
        return Result.success("点赞成功!", null);
    }

    // 判断用户是否已经点赞
    @RequestMapping("getArgee")
    @ResponseBody
    Result getArgee(int aid,@SessionAttribute User myinfo){
        SetOperations set = redisTemplate.opsForSet();
        Boolean member = set.isMember("article-argee:" + aid, myinfo.getId());
        return member?
                Result.success("已点赞",null)
                :Result.failure("未点赞",null);
    }

    // 取消点赞
    @RequestMapping("removeArgee")
    @ResponseBody
    Result removeArgee(int aid, @SessionAttribute User myinfo){
        SetOperations set = redisTemplate.opsForSet();
        set.remove("article-argee:"+ aid, myinfo.getId());
        return Result.success("取消点赞成功!", null);
    }

    // 获取点赞数
    @RequestMapping("getArgeeCnt")
    @ResponseBody
    Result getArgeeCnt(int aid){
        SetOperations set = redisTemplate.opsForSet();
        return Result.success("点赞数!", set.size("article-argee:"+ aid));
    }

    // 扩展完成点赞数更新问题: 定时任务 24 消失更新数据库中文章点赞数

}

